$(function () {

})

function sendEmail() {
    let email = $.trim($("#email").val());
    if (email.length == 0) {
        scrollToDiv("#email");
        layer.tips('Please Enter Your Email！', '#email', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    if (!commonClass.validEmail(email)) {
        layer.tips('Your email is not in the correct format', '#email', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    $.ajax({
        url: "/send_email",
        type: "post",
        data: {
            email: email
        },
        success: function (response) {
            if (response.code == 200) {
                layer.msg("We have send an email to you,please check your mailbox", {
                    icon: 1
                });
                commonClass.countDown(60, "btnSendEmail", "Send Email");
                return;
            } else {
                layer.msg(response.message, {
                    icon: 2
                });
            }
        },
        error: function () {
            layer.msg("Network Error", {
                icon: 2
            });
        },
        beforeSend: function () {
            showProgress();
        },
        complete: function () {
            layer.closeAll('loading');
        }
    })
}

function scrollToDiv(dom) {
    $('html, body').animate({
        scrollTop: $(dom).offset().top - 200
    }, 500);
}

function updatePassword() {
    let password = $("#password").val();
    let passwordConfirm = $("#passwordConfirm").val();
    let validCode = $("#validCode").val();
    if ($.trim(password).length == 0) {
        scrollToDiv("#password");
        layer.tips('Please enter your password！', '#password', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }
    if (password.length < 6) {
        scrollToDiv("#password");
        layer.tips('Password length is at least 6 digits！', '#password', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if ($.trim(passwordConfirm).length == 0) {
        scrollToDiv("#passwordConfirm");
        layer.tips('Please confirm your password！', '#passwordConfirm', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if (password != passwordConfirm) {
        scrollToDiv("#passwordConfirm");
        layer.tips('The password you entered twice is inconsistent！', '#passwordConfirm', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    if ($.trim(validCode).length != 6) {
        scrollToDiv("#validCode");
        layer.tips('The valid code cannot be null,and it should be 6 digits', '#validCode', {
            tips: [1, '#ff0000'],
            time: 4000
        });
        return;
    }

    $.ajax({
        url: "/update_password",
        type: "post",
        data: {
            validCode: validCode,
            password: md5(password)
        },
        success: function (response) {
            if (response.code == 200) {
                layer.alert("update your message successfully!we will jump to the login page", {
                    skin: 'layui-layer-molv' //样式类名
                    , closeBtn: 0
                }, function () {
                    location.href = "/login.html";
                });
                return;
            } else {
                layer.msg(response.message, {
                    icon: 2
                });
            }
        },
        error: function () {
            layer.msg("Network Error", {
                icon: 2
            });
        },
        beforeSend: function () {
            showProgress();
        },
        complete: function () {
            layer.closeAll('loading');
        }
    })

}

function showProgress() {
    layer.load(0, {
        shade: [0.5, '#fff'] //0.1透明度的白色背景
    });
}